const express = require('express')

const app = express()

//路由
app.use((req, res, next) => {
    res.cc = function(err, status = 1) {
        res.send({
            status, 
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})

const userRouter = require('./router/user.js')
app.use('/api', userRouter)



var plateData = {carNumber:'川ADC7890'}
let getData = (ReqData)=>{//抓页面
//  app.get("/select",function(req,res){
//     console.log('token',req.session)
//     res.send()
//  })
    // console.log('getData cookie', ReqData)
    var time = new Date().getTime()
    return new Promise((resolve,reject)=>{
    var sign = signGenerate(plateData)
    // console.log('sign',sign)
    superagent.post(get_url)
    // .set("Cookie", ReqData.cookie)
    .set('Content-Type','application/x-www-form-urlencoded;charset=UTF-8')
    .set('origin','https://mp-merchant.fleetingpower.com')
    .set('user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36')
    .set('x-app-key','broker_portal')
    .set('sign',sign)
    .set('authority','mecs.fleetingpower.com')
    .set('path','/services/v3/gaiaMobile/car/query')
    .set('scheme','https')
    .set('timestamp',time)
    .set('x-perm-id',ReqData.xPermId)
    .set('x-broker-token',ReqData.token+1)
    .set('referer', 'https://mp-merchant.fleetingpower.com/')
    .send(plateData)
    .end((err,response)=>{
      console.log('response.session', response.session)
      var data = JSON.parse(response.text)
      console.log(response.text)
      if(data.code!=200 || !data.result){
        reject(err)
        return data
      };
      var res= {}
      res = {...data.result[0], xPermId: response.headers['x-perm-id'], token: ReqData.token}
      // console.log('getData response',res)
      resolve(res);
    })
    })
}


app.listen(3077, ()=> {
    console.log('listen 3077')
})